package cn.dazhou.wificonnectdemo.wifi

import android.os.Handler
import android.os.Looper
import android.os.Message
import cn.dazhou.wificonnectdemo.Timber

/** wifi扫描Handler，用于实现延迟扫描、循环扫描 */
object WifiScanHandler : Handler(Looper.getMainLooper()) {

    private const val what = 110
    private const val delay = 10 * 1000L

    override fun handleMessage(msg: Message) {
        Timber.fi("调用了扫描wifi函数")
        startScan()
    }

    /** 开始扫描wifi */
    fun startScan() {
        @Suppress("DEPRECATION")
        WifiUtil.wifiManager.startScan()
    }

    /** 延迟指定的时间后开始扫描wifi（默认延迟10秒） */
    fun delayScan(delay: Long = this.delay) {
        removeMessages(what) // 预防重复
        sendEmptyMessageDelayed(what, delay)
    }

    /** 移除扫描 */
    fun removeScan() {
        removeMessages(what)
    }

}